/*
 * DataClip.h
 *
 *  Created on: Mar 29, 2013
 *      Author: root
 */

#ifndef DATACLIP_H_
#define DATACLIP_H_

#include "MPEGTS.h"
#include "http.h"

/*
*max file clip num
*/
#define MAX_CLIP_NUM 0xff
#define INVALID_HANDLE_VALUE -1
/*
 * file fd list struct
 */
struct hFile_info
{
    int   	 hFile; /*file handle*/
    uint64_t hFile_size;/*hFile size*/
    uint64_t hFile_offset;/*hFile offset*/
    uint64_t trans_size;/*total trans byte size */
	char     hFileName[FILE_MAX_PATH];/*file name*/

    hFile_info()
    {
    	hFile = INVALID_HANDLE_VALUE;
        hFile_size = 0;
        hFile_offset = 0;
        trans_size = 0;
		memset(hFileName,'\0',sizeof(hFileName));
    }
};


/*
*token ex struct
*/
struct token_ex
{
	int year;
	int month;
	int day;
	int hours;
	int minit;
	int seconds;/*seconds*/
	int flag;
	int duration;/*seconds*/

	bool is_live;
	bool is_init;

	char stream_name[128];
	hFile_info filelist[MAX_CLIP_NUM];/*define max file count*/
    int nb_file_num; /*descript filelist nb file number*/
    int nb_file_index;/*descript current op file index in the filelist*/
	uint64_t total_file_size;/*filelist total size*/
	uint64_t total_offset;/*total offset*/
	uint64_t total_offset_end;/*total offset end*/
	uint64_t total_trans_size;/*filelist total trans size*/
	uint64_t total_have_trans_size;/*filelist have total trans size*/
	bool is_rang;

	token_ex()
	{
		year = 1900;
		month = 0;
		day = 0;
		hours = 0;
		minit = 0;
		seconds = 0;
		flag = 0;
		duration = 0;
		is_live = false;
		is_init = false;
		memset(stream_name,'\0',sizeof(stream_name));
		memset(filelist,NULL,sizeof(filelist));
        nb_file_num = 0;
        nb_file_index = 0;
		total_file_size = 0;
		total_offset = 0;
		total_offset_end = 0;
		total_trans_size = 0;
		total_have_trans_size = 0;
		is_rang = false;
	}
};

/**
 * DataClip class
 */
class DataClip {
public:
	DataClip();
	virtual ~DataClip();

	/*
	*init clip args
	*clip_duration:seconds(10~3600)
	*/
	static void init(int clip_duration = 60);

	/*
	*reset token ex
	*/
	static int reset_token_ex(token_ex * ptokenEx);

	/*
	*
	*set datetime
	*
	*datetime:2012-10-31-14-31-00,yyyy-month-day-hour-min-second
	*
	*/
	static bool set_token_datetime(token_ex * ptokenEx,char * datetimestr);


	/*
	*get clip index by date time
	*/
	static uint16_t get_clip_index(uint16_t & tyear,uint8_t & tmonth,uint8_t & tday);

	/*
	*calculate clip index by time
	*/
	static uint16_t calculate_clip_index(uint16_t thour,uint8_t tmin,uint8_t tseconds);

	/*
	*calculate clip begin time
	*/
	static void calculate_clip_begintime(uint16_t clipindex,token_ex * ptokenEx);

	/*
	*calculate clip begin time
	*seconds,0~86400
	*/
	static int calculate_clip_begintime(uint16_t clipindex);

	/*
	*calculate clip begin time
	*seconds,0~86400
	*/
	static int conversion_to_seconds(uint16_t thour,uint8_t tmin,uint8_t tseconds);


	/*
	*create clip file name
	*/
	static bool create_clip_file_name(uint16_t streamid,int fmt,uint16_t tyear,uint8_t tmonth,uint8_t tday, uint16_t clipindex,const char * root,char * buf ,size_t buflen);

	/*
	*open clip list file
	*/
	static bool open_clip_file_list(uint16_t streamid,int fmt,token_ex * ptoken,const char * root);

	/*
	*open next clip file
	*/
	static int open_next_clip_file(uint16_t streamid,int fmt,token_ex * ptoken,const char * root,uint64_t & hFile_offset,uint64_t & hFile_size);

	/*
	*del data section clip by day
	*/
	static bool del_clip_file_list(uint16_t streamid,int fmt,uint8_t before_tdays,const char * root,int &error);

	/**
	 * get file size
	 */
	static unsigned long  get_file_size(int fd);

};

#endif /* DATACLIP_H_ */
